<?php
namespace RMD\AccountBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use RMD\AccountBundle\Form\Type\TestType;
use RMD\AccountBundle\Form\Type\OperationType;
use RMD\AccountBundle\Form\Type\ListSegmentType;
use RMD\AccountBundle\Form\Type\SegmentType;

use RMD\AccountBundle\Entity\GroupSet;
use RMD\AccountBundle\Entity\ListSegment;


class CampaignsController extends Controller
{
	public function recipientAction(){
		$form = $this->createForm(new ListSegmentType(), $this->getUser()->getLists());
		$list = $this->getDoctrine()->getRepository('RMDAccountBundle:Lists')->find(1);
		//$segmentForm = $this->createForm(new ListSegmentType(), $list);
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			//$form->bind($request);
			echo $form->getName();
			var_dump($request->request->all());
		}
		return $this->render('RMDAccountBundle:Campaigns:recipient.html.php',  array('form' => $form->createView()));
	}
	public function segmentAction(){
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			$postData = $this->getPostData(array('listid'));
			if (!$postData) {
				throw $this->createNotFoundException('Bad POST data');
			}
			$list = $this->getDoctrine()->getRepository('RMDAccountBundle:Lists')->find((int)$postData['listid']);
			if (!$list || !$list->isOwnedBy($this->getUser())) {
				throw $this->createNotFoundException('Access violation');
			}
			$segment = new ListSegment($list);
			$form = $this->createForm(new ListSegmentType(), null, array('segmentOptions' => $segment->getOptions()));
			return $this->render('RMDAccountBundle:Campaigns:segment.html.php',  array('form' => $form->createView()));
		}
		throw $this->createNotFoundException('Only POST request accepted');
	}

	public function operationAction($listid = 1, $field = 'email'){
		$request = $this->getRequest();
		if ($request->isMethod('POST')) {
			$postData = $this->getPostData(array('listid', 'field'));
			if (!$postData) {
				throw $this->createNotFoundException('Bad POST data');
			}
			$list = $this->getDoctrine()->getRepository('RMDAccountBundle:Lists')->find((int)$postData['listid']);
			if (!$list || !$list->isOwnedBy($this->getUser())) {
				throw $this->createNotFoundException('Access violation');
			}
			$segment = new ListSegment($list);
			$form = $this->createForm(new ListSegmentType(), null, array('operations' => $segment->getOption($postData['field'])));
			return $this->render('RMDAccountBundle:Campaigns:operation.html.php',  array('form' => $form->createView()));
		}
		throw $this->createNotFoundException('Only POST request accepted');
	}
	private function getPostData($mandatoryParams = array(), $fromJson = true){
		$request = $this->getRequest();
		$postData = $request->getContent();
		if ($fromJson) {
			$postData = json_decode($postData, true);
		}
		if (is_array($mandatoryParams)) {
			foreach ($mandatoryParams as $param) {
				if (!isset($postData[$param])) {
					return false;
				}
			}
		}
		return $postData;		
	}
}
